package com.situ.crm.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.crm.bean.pojo.Permission;
import com.situ.crm.bean.pojo.RolePermission;
import com.situ.crm.bean.pojo.User;
import com.situ.crm.bean.vo.PermissionVO;
import com.situ.crm.bean.vo.RouterVO;
import com.situ.crm.common.utils.ConvertUtils;
import com.situ.crm.rbac.mapper.PermissionMapper;
import com.situ.crm.rbac.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.crm.rbac.service.IRolePermissionService;
import com.situ.crm.rbac.utils.PermissionTreeUtil;
import com.situ.crm.rbac.utils.RouterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2022-11-10
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {
    @Autowired
    private IRolePermissionService rolePermissionService;

    //获得全部菜单的树形结构
    @Override
    public List<PermissionVO> selectAll() {
        //1.查找菜单表里面所有的数据
        List<Permission> permissionList = baseMapper.selectList(new QueryWrapper<Permission>().orderByAsc("sort"));
        List<PermissionVO> permissionVOList = ConvertUtils.convertList(permissionList, PermissionVO.class);
        //2.按照父子关系构造树形结构
        List<PermissionVO> treeList = PermissionTreeUtil.buildTree(permissionVOList);
        return treeList;
    }

    @Override
    public Map<String, Object> selectAssignedPermission(Long roleId) {
        List<PermissionVO> permissionVOTreeList = selectAll();
        //根据角色roleId查询这个角色下面所有的权限
        List<RolePermission> rolePermissionList = rolePermissionService.list(new QueryWrapper<RolePermission>().eq("role_id", roleId).select("permission_id"));
        List<Long> assignedPermissionIdList = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            assignedPermissionIdList.add(rolePermission.getPermissionId());
        }

        Map<String, Object> map = new HashMap<>();
        map.put("permissionVOTreeList", permissionVOTreeList);
        map.put("assignedPermissionIdList", assignedPermissionIdList);
        return map;
    }

    @Override
    public void assignPermission(Long roleId, Long[] permissionIds) {
        rolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id", roleId));
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(rolePermissionList);
    }

    @Override
    public List<RouterVO> selectRouterListByUser(User user) {
        List<Permission> permissionList = null;
        //user的名字是admin的是超级管理员,具有所有的权限
        if (user.getName().equalsIgnoreCase("admin")) {
            permissionList = baseMapper.selectList(new QueryWrapper<Permission>().orderByAsc("sort"));
        } else {
            permissionList = baseMapper.selectPermissionByUserId(user.getId());
        }
        List<PermissionVO> permissionVOList = ConvertUtils.convertList(permissionList, PermissionVO.class);
        //构件树形结构
        List<PermissionVO> permissionVOTreeList = PermissionTreeUtil.buildTree(permissionVOList);
        //转换成前端路由需要的格式
        List<RouterVO> routerVOList = RouterUtils.build(permissionVOTreeList);

        return routerVOList;
    }

    @Override
    public List<String> selectBtnListByUser(User user) {
        List<Permission> permissionList = null;
        //user的名字是admin的是超级管理员,具有所有的权限
        if (user.getName().equalsIgnoreCase("admin")) {
            permissionList = baseMapper.selectList(new QueryWrapper<Permission>().orderByAsc("sort"));
        } else {
            permissionList = baseMapper.selectPermissionByUserId(user.getId());
        }
        List<String> btnPermissionList = new ArrayList<>();
        for (Permission permission : permissionList) {
            if (permission.getType() == 2) {//按钮权限
                btnPermissionList.add(permission.getPermissionValue());
            }
        }
        return btnPermissionList;
    }
}
